package com.dp.appi.dao.impl;

import com.dp.appi.dao.ConsumerDao;
import com.dp.appi.entity.UserEntity;
import com.dp.appi.util.StringUtil;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaoji on 2016/4/20.
 */
@Component
public class ConsumerDaoImpl extends BaseDaoImpl implements ConsumerDao{
    /**
     * 根据账号获取用户对象
     *
     * @param userEntity 用户登录对象
     * @return
     */
    @Override
    public Map<String, Object> getUser(UserEntity userEntity) {
        String sql = "select *,is_vip as isVip,UNIX_TIMESTAMP(vip_start) as vipStart,UNIX_TIMESTAMP(vip_end) as vipEnd,UNIX_TIMESTAMP(NOW()) as currentDate" +
                " from t_s_base_user tsb inner join t_s_user tsu on tsb.ID=tsu.id" +
                " where tsb.username = ? and tsu.mobilePhone=? and tsb.password=? ";
        List<Map<String,Object>> list = getJdbcTemplate().query(sql,new Object[]{userEntity.getA(),userEntity.getB(),userEntity.getC()},getColumnMapRowMapper());
        if(list.size() == 1){
            return list.get(0);
        }else{
            return null;
        }
    }

    @Override
    public Map<String, Object> getUserByUsername(String username) {
        String sql="select a.*,b.org_id from t_s_base_user a left join t_s_user_org b on a.ID = b.user_id where username= ?";
        List<Map<String,Object>> list = getJdbcTemplate().queryForList(sql, username);
        if(list!=null&&list.size()>0){
            return list.get(0);
        }
        return null;
    }

    /**
     * @param param
     */
    @Override
    public void updateUser(Map<String, String> param) {
        String id = param.get("id");
        String realname=param.get("realname");
        if(!StringUtil.isEmpty(realname)){
            String sql = "update t_s_base_user set realname=? where ID=?";
            getJdbcTemplate().update(sql,new Object[]{realname,id});
        }
        String sql2 = "";
        List<String> list = new ArrayList<>();
        String avatar=param.get("avatar");
        if(!StringUtil.isEmpty(avatar)){
            sql2+="avatar=?,";
            list.add(avatar);
        }
        String email=param.get("email");
        if(!StringUtil.isEmpty(email)){
            sql2+="email=?,";
            list.add(email);
        }
        String qq=param.get("qq");
        if(!StringUtil.isEmpty(qq)){
            sql2+="qq=?,";
            list.add(qq);
        }
        String wx=param.get("wx");
        if(!StringUtil.isEmpty(wx)){
            sql2+="wx=?,";
            list.add(wx);
        }
        String birthday=param.get("birthday");
        if(!StringUtil.isEmpty(birthday)){
            sql2+="birthday=?,";
            list.add(birthday);
        }
        String sex=param.get("sex");
        if(!StringUtil.isEmpty(sex)){
            sql2+="sex=?,";
            list.add(sex);
        }
        if(!StringUtil.isEmpty(sql2)){
            sql2 = sql2.substring(0,sql2.length()-1);
            list.add(id);
            String sql3 = "update t_s_user set "+sql2+" where id=?";
            getJdbcTemplate().update(sql3,list.toArray());
        }

    }

    /**
     * 根据用户获取用户组织机构
     * @param userId
     * @return
     */
    @Override
    public Map<String, Object> getTSpartByUserId(String userId) {
        String sql = "select tsuo.*,tsd.org_type from t_s_user_org tsuo " +
                " LEFT JOIN t_s_depart tsd on tsuo.org_id=tsd.ID " +
                " where tsuo.user_id=?";
        List<Map<String,Object>> list = getJdbcTemplate().query(sql, new Object[]{userId},getColumnMapRowMapper());
        if(list.size() == 1){
            return list.get(0);
        }else{
            return null;
        }
    }
}
